// 数据验证中间件

const { checkNull, isJson, handleNumber, checkPhone, checkMail, checkTimeStamp } = require("../rules/verification")

// vari : verification 简写 中文：验证
const veri = {
    username: (ctx, next) => {
        let { username } = ctx.request.body || "";
        let checkedusername = checkNull(username, "账号")
        if (checkedusername.code != "200") return ctx.body = checkedusername
        return next()
    },
    password: (ctx, next) => {
        let { password } = ctx.request.body || "";
        let checkedpassword = checkNull(password, "密码")
        if (checkedpassword.code != "200") return ctx.body = checkedpassword
        return next()
    },
    page: (ctx, next) => {
        const { pageNum, pageSize } = ctx.query || "";
        let checkedNum = handleNumber(pageNum, "pageNum")
        if (checkedNum.code !== "200") return ctx.body = checkedNum
        let checkedSize = handleNumber(pageSize, "pageSize")
        if (checkedSize.code !== "200") return ctx.body = checkedSize
        return next()
    },
    id: (ctx, next) => {
        const { id } = ctx.params || "";
        let checkedId = checkNull(id, "id")
        if (checkedId.code !== "200") return ctx.body = checkedId
        return next()
    },
    role_id: (ctx, next) => {
        const { role_id } = ctx.request.body || "";
        let checkedrole_id = handleNumber(role_id, "role_id")
        if (checkedrole_id.code !== "200") return ctx.body = checkedrole_id
        return next()
    },
    sex: (ctx, next) => {
        const { sex } = ctx.request.body || "";
        if (sex) {
            let checkedsex = checkNull(sex, "性别", 2)
            if (checkedsex.code !== "200") return ctx.body = checkedsex
        }
        return next()
    },
    phone: (ctx, next) => {
        const { phone } = ctx.request.body || "";
        if (phone) {
            let checkedphone = checkPhone(phone, "手机号码")
            if (checkedphone.code !== "200") return ctx.body = checkedphone
        }
        return next()
    },
    mail: (ctx, next) => {
        const { mail } = ctx.request.body || "";
        if (mail) {
            let checkedmail = checkMail(mail, "邮箱")
            if (checkedmail.code !== "200") return ctx.body = checkedmail
        }
        return next()
    },
    rulename: (ctx, next) => {
        const { rulename } = ctx.request.body || "";
        let checkedRulename = checkNull(rulename, "权限名称")
        if (checkedRulename.code !== "200") return ctx.body = checkedRulename
        return next()
    },
    summary: (ctx, next) => {
        const { summary } = ctx.request.body || "";
        let checkedSummary = checkNull(summary, "权限概述")
        if (checkedSummary.code !== "200") return ctx.body = checkedSummary
        return next()
    },
    rolename: (ctx, next) => {
        const { rolename } = ctx.request.body || "";
        let checkedrolename = checkNull(rolename, "角色名称")
        if (checkedrolename.code !== "200") return ctx.body = checkedrolename
        return next()
    },
    rolekey: (ctx, next) => {
        const { rolekey } = ctx.request.body || "";
        let checkedrolekey = checkNull(rolekey, "角色权限")
        if (checkedrolekey.code !== "200") return ctx.body = checkedrolekey
        return next()
    },

}


module.exports = {
    veri
}